﻿using System.Diagnostics;

namespace LxBasic
{
    public struct CellGeomCalc
    {
        private double volumnP;
        private Vector3d centerP;
        public void AddFace(Vector3d normal, Vector3d center)
        {
            double volWeight = Vector3d.DotProduct(normal, center);
            volumnP += volWeight;
            centerP += volWeight * center;
        }
        public void AddFaceReversed(Vector3d normal, Vector3d center)
        {
            double volWeight = -Vector3d.DotProduct(normal, center);
            volumnP += volWeight;
            centerP += volWeight * center;
        }
        public (double Volumn, Vector3d Center) GetResult()
            => (volumnP / 3, 0.75 / volumnP * centerP);
#if DEBUG
        public static void Test()
        {
            test1();
            test2();
        }
        public static void test1()
        {
            CellGeomCalc c = new CellGeomCalc();
            c.AddFace(new Vector3d(0, 0, 0.5), new Vector3d(1.0 / 3, 1.0 / 3, 1));
            c.AddFace(new Vector3d(1, 1, 0), new Vector3d(0.5, 0.5, 0.5));
            var r = c.GetResult();
            Console.WriteLine($"vol:{r.Volumn};center:{r.Center}");
        }

        public static void test2()
        {
            CellGeomCalc c = new CellGeomCalc();
            c.AddFace(new Vector3d(0, 0, 1), new Vector3d(0.5, 0.5, 1));
            c.AddFaceReversed(new Vector3d(0, 0, 1), new Vector3d(0.5, 0.5, 0));
            c.AddFace(new Vector3d(1, 0, 0), new Vector3d(1, 0.5, 0.5));
            c.AddFaceReversed(new Vector3d(1, 0, 0), new Vector3d(0, 0.5, 0.5));
            c.AddFace(new Vector3d(0, 1, 0), new Vector3d(0.5, 1, 0.5));
            c.AddFaceReversed(new Vector3d(0, 1, 0), new Vector3d(0.5, 0, 0.5));
            var r = c.GetResult();
            Console.WriteLine($"vol:{r.Volumn};center:{r.Center}");
        }
#endif
    }
}


